<?php

namespace app\common\model;

//use think\Model;

class ExchangeConfigModel extends BaseModel{

	protected $table = 'tp_exchange_config';

	/*
	兑换列表
	 */
	public function exList($uid){
		$data = $this->alias('e')
						->join('tp_coin_wallet w','w.id=e.wid and w.status=1')
						->join('tp_user_balance b','b.wid=e.wid and b.uid='.$uid,'LEFT')
						->where('e.status',1)	
						->group('e.wid')
						->order('e.sort asc')
						->field('e.wid,w.icon,w.name,w.price,b.balance')
						->select();
		if(empty($data)){
			return [];
		}
		foreach ($data as $key => $val) {
			$data[$key]['balance'] = $data[$key]['balance']?:0;
			$data[$key]['balance'] = $data[$key]['balance']/1;

			$to = $this->alias('e')
							->join('tp_coin_wallet w1','w1.id=e.to_wid and w1.status=1')
							->join('tp_coin_wallet w2','w2.id=e.fee_wid and w2.status=1')
							->join('tp_user_balance b','b.wid=e.to_wid and b.uid='.$uid,'LEFT')
							->where('e.wid',$val['wid'])
							->where('e.status',1)
							->order('e.sort asc')
							->field('e.*,w1.icon,w1.name,w1.price,w2.name fee_name,b.balance to_balance')
							->select()?:[];
			if ($to) {
				foreach ($to as $k => $v) {
					$to[$k]['to_balance'] = $to[$k]['to_balance']?:0;
					$to[$k]['to_balance'] = $to[$k]['to_balance']/1;
					$to[$k]['rate'] = round($val['price']/$v['price'],4);
				}
			}
			$data[$key]['to'] = $to;
		}
		return  $data;
	}

	
	
	/*
	单个钱包兑换列表
	 */
	public function exOne($uid,$wid){
		$data = $this->alias('e')
						->join('tp_coin_wallet w','w.id=e.wid and w.status=1')
						->join('tp_user_balance b','b.wid=e.wid and b.uid='.$uid,'LEFT')
						->where('e.status',1)	
						->where('e.wid',$wid)
						->field('e.wid,w.icon,w.name,w.price,b.balance')
						->find();
		if(empty($data)){
			return [];
		}
		$data['balance'] = $data['balance']?:0;
		$data['balance'] = $data['balance']/1;

		$to = $this->alias('e')
						->join('tp_coin_wallet w1','w1.id=e.to_wid and w1.status=1')
						->join('tp_coin_wallet w2','w2.id=e.fee_wid and w2.status=1')
						->join('tp_user_balance b','b.wid=e.to_wid and b.uid='.$uid,'LEFT')
						->where('e.wid',$data['wid'])
						->where('e.status',1)
						->order('e.sort asc')
						->field('e.*,w1.icon,w1.name,w1.price,w2.name fee_name,b.balance to_balance')
						->select()?:[];
		if ($to) {
			foreach ($to as $k => $v) {
				$to[$k]['to_balance'] = $to[$k]['to_balance']?:0;
				$to[$k]['to_balance'] = $to[$k]['to_balance']/1;
				$to[$k]['rate'] = round($data['price']/$v['price'],4);
			}
		}
		$data['to'] = $to;
		return  $data;
	}
	/*
	获取一条配置
	 */
	public function getOneConfig($id,$uid){
		$data = $this->alias('e')
						->join('tp_coin_wallet w1','w1.id=e.wid and w1.status=1')
						->join('tp_coin_wallet w2','w2.id=e.to_wid and w2.status=1')
						// ->join('tp_coin_wallet w3','w3.id=e.fee_wid and w3.status=1')
						->join('tp_user_balance b','b.wid=e.wid and b.uid='.$uid,'LEFT')
						->where('e.id',$id)	
						->where('e.status',1)
						->field('e.*,w1.price from_price,w2.price to_price,b.balance')
						->find();
		if ($data) {
			$data['balance'] = $data['balance']?:0;
		}
		return $data;
	}
}